package com.zluyuer.ermu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DemoMessageTest {

    public static void main(String[] args) {
        
        try {
            // Register message
            ErmuHelper.register(DemoMessage.class, new DemoMessageHandler());
            
            // Launch server in another thread
            final ErmuServer server = new ErmuServer(3333);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        server.startup();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            
            // Wait for server launching over
            Thread.sleep(1000);
            
            // Launch client
            ErmuClient client = new ErmuClient();
            client.connect("127.0.0.1", 3333);
            
            // Prepare data to send
            DemoMessage m = new DemoMessage();
            m.setStr("<html>welcome</html>");
            m.setNum(2013);
            List<String> strlist = new ArrayList<String>();
            strlist.add("Alice");
            strlist.add("Bob");
            m.setStrlist(strlist);
            List<Integer> numlist = new ArrayList<Integer>();
            numlist.add(1949);
            numlist.add(2013);
            m.setNumlist(numlist);
            Map<String, Integer> map = new HashMap<String, Integer>();
            map.put("England", 947);
            map.put("Norway", 360);
            System.out.println(m.toXML());
            m.setMap(map);
            
            // Send message
            client.send(m);
            
            // Game over
            client.close();
            server.close();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
